/*******************************************************************************
 * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
 * @file     transport.c
 * @author   UartTFT Application Team
 * @version  V0.0.1
 * @date     2023-01-01
 * @brief
 ********************************************************************************/

#include "transport.h"
#include "delay.h"
#include "w25qxx.h"
#include "w25n01.h"
#include "module_select.h"
#include "levetop.h"
#include "tftlcd_spi.h"
#include "tftlcd_ebi_8bit.h"
#include "tftlcd_ebi_16bit.h"
#include "tftlcd_qspi.h"

extern uint8_t lt_ui_change;

void LT_UI_changeaddr(uint32_t *addr)
{
	if (lt_ui_change == 0)
	{
		*addr += 0;
	}
	else if (lt_ui_change == 1)
	{
		*addr += 0;
	}
	else if (lt_ui_change == 2)
	{
		*addr += 0;
	}
	else if (lt_ui_change == 3)
	{
		*addr += 0;
	}
}

void LT_ReadFlash_UI(uint8_t *pBuffer, uint32_t ReadAddr, uint32_t NumByteToRead)
{
	LT_UI_changeaddr(&ReadAddr);
	
	if (Flash_Type < 2)
	{
		NorFlash_Read(pBuffer, ReadAddr, NumByteToRead);
	}
	else if (Flash_Type == 2)
	{
		uint16_t page, column, pageremain;

		page = (ReadAddr / 2048);
		column = (ReadAddr % 2048);
		pageremain = 2048 - column;
		// printf("page column pageremain %d   %d %d \r\n",page,column,pageremain);

		if (NumByteToRead <= pageremain)
			pageremain = NumByteToRead;
		for (;;)
		{
			NandFlash_Read(page, column, pBuffer, pageremain);
			if (NumByteToRead == pageremain)
				break;
			else
			{
				pBuffer += pageremain;
				page++;
				column = 0;
				NumByteToRead -= pageremain;
				if (NumByteToRead > 2048)
					pageremain = 2048;
				else
					pageremain = NumByteToRead;
			}
		}
	}
	else if (Flash_Type == 3)
	{

	}

//	DelayUS(1); // wav need
}

void LT_ReadFlash_DMA_UI(uint8_t *pBuffer, uint32_t ReadAddr, uint32_t NumByteToRead)
{
	LT_UI_changeaddr(&ReadAddr);

	if (Flash_Type < 2)
	{
		uint32_t dma_len = DMA_LEN3;

		if (NumByteToRead <= dma_len)
			dma_len = NumByteToRead;
		for (;;)
		{
			NorFlash_DMA_Read(pBuffer, ReadAddr, dma_len); //DUAL DMA
			NorFlash_DMA_Wait();
			//DelayUS(1); // wav need

			if (NumByteToRead == dma_len)
				break;
			else
			{
				pBuffer += dma_len;
				ReadAddr += dma_len;
				NumByteToRead -= dma_len;
				if (NumByteToRead > DMA_LEN3)
					dma_len = DMA_LEN3;
				else
					dma_len = NumByteToRead;
			}
		}
	}
	else if (Flash_Type == 2)
	{
		uint16_t page, column, pageremain;

		page = (ReadAddr / 2048);
		column = (ReadAddr % 2048);
		pageremain = 2048 - column;

		if (NumByteToRead <= pageremain)
			pageremain = NumByteToRead;
		for (;;)
		{
			NandFlash_DMA_Read(page, column, pBuffer, pageremain);
			NandFlash_DMA_Wait();
			if (NumByteToRead == pageremain)
				break;
			else
			{
				pBuffer += pageremain;
				page++;
				column = 0;

				NumByteToRead -= pageremain;
				if (NumByteToRead > 2048)
					pageremain = 2048;
				else
					pageremain = NumByteToRead;
			}
		}
	}
	else if (Flash_Type == 3)
	{

	}

//	DelayUS(1); // wav need
}

/********************************************************************************
* Function Name	: LT_ReadFlash
* Description  	: Read data from external FLASH.
* Input        	: - *pBuffer: buffer for read data
				  - ReadAddr: Address to start reading
				  - NumByteToRead: Number of bytes to read
* Output       	: None
* Return       	: None
*********************************************************************************/
void LT_ReadFlash(uint8_t *pBuffer, uint32_t ReadAddr, uint32_t NumByteToRead)
{
	if (Flash_Type < 2)
	{
		NorFlash_Read(pBuffer, ReadAddr, NumByteToRead);
	}
	else if (Flash_Type == 2)
	{
		uint16_t page, column, pageremain;

		page = (ReadAddr / 2048);
		column = (ReadAddr % 2048);
		pageremain = 2048 - column;
		// printf("page column pageremain %d   %d %d \r\n",page,column,pageremain);

		if (NumByteToRead <= pageremain)
			pageremain = NumByteToRead;
		for (;;)
		{
			NandFlash_Read(page, column, pBuffer, pageremain);
			if (NumByteToRead == pageremain)
				break;
			else
			{
				pBuffer += pageremain;
				page++;
				column = 0;
				NumByteToRead -= pageremain;
				if (NumByteToRead > 2048)
					pageremain = 2048;
				else
					pageremain = NumByteToRead;
			}
		}
	}
	else if (Flash_Type == 3)
	{


	}

//	DelayUS(1); // wav need
}

/********************************************************************************
* Function Name	: LT_ReadFlash_DMA
* Description  	: Read data from external FLASH.
* Input        	: - *pBuffer: buffer for read data
				  - ReadAddr: Address to start reading
				  - NumByteToRead: Number of bytes to read
* Output       	: None
* Return       	: None
*********************************************************************************/
void LT_ReadFlash_DMA(uint8_t *pBuffer, uint32_t ReadAddr, uint32_t NumByteToRead)
{
	if (Flash_Type < 2)
	{
		uint32_t dma_len = DMA_LEN3;

		if (NumByteToRead <= dma_len)
			dma_len = NumByteToRead;
		for (;;)
		{
			NorFlash_DMA_Read(pBuffer, ReadAddr, dma_len); //DUAL DMA
			NorFlash_DMA_Wait();
			//DelayUS(1); // wav need

			if (NumByteToRead == dma_len)
				break;
			else
			{
				pBuffer += dma_len;
				ReadAddr += dma_len;
				NumByteToRead -= dma_len;
				if (NumByteToRead > DMA_LEN3)
					dma_len = DMA_LEN3;
				else
					dma_len = NumByteToRead;
			}
		}
	}
	else if (Flash_Type == 2)
	{
		uint16_t page, column, pageremain;

		page = (ReadAddr / 2048);
		column = (ReadAddr % 2048);
		pageremain = 2048 - column;

		if (NumByteToRead <= pageremain)
			pageremain = NumByteToRead;
		for (;;)
		{
			NandFlash_DMA_Read(page, column, pBuffer, pageremain);
			NandFlash_DMA_Wait();
			if (NumByteToRead == pageremain)
				break;
			else
			{
				pBuffer += pageremain;
				page++;
				column = 0;

				NumByteToRead -= pageremain;
				if (NumByteToRead > 2048)
					pageremain = 2048;
				else
					pageremain = NumByteToRead;
			}
		}
	}
	else if (Flash_Type == 3)
	{

	}

//	DelayUS(1); // wav need
}

//----------------------------------------------------------------------------------
